package com.shengyuan.manage_os.old.ying.erpm;

import org.json.JSONException;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.controls.PictureButton;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class Actual_OA_CaiGouDan1 extends BaseActivity {

	private Spinner spinner_ziyuanxiangmu;
	protected EditText editText_yourself,editText_jinqian, editText_shuliang, editText_danjia;
	private PictureButton PictureButton_yourself;
	

	@Override
	public void onClick(View view) {
		super.onClick(view);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.actual_oa_caigoudan);
		//InitControls();
	}

	@Override
	protected void SaveJsonData() {
		try {
			IsJson1 = true; mFinished = 1; mPrintenabled = 0;
			JsonMyID();
			getTask();
			getDataExt();
			yourself();
			
			getGPS();
			getCurrentUser();		
			getMemo();
			JsonPicture();
			save_pic();
		}catch (Exception e) {
		}
	}
	
	protected void yourself() throws JSONException {
		if (editText_yourself!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_yourself_zhikuan), editText_yourself.getText().toString());
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		if (PictureButton_yourself!=null) {
			PictureButton_yourself.postInvalidate();
		}
		
	}
	private void save_pic() throws JSONException {
		if (PictureButton_yourself!=null) {
			mJson.put( PictureButton_yourself.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_yourself.mFileName);
		}
	}
	
	@Override
	protected void SaveSelfPic(String sPath) {
		try {
			if (PictureButton_yourself!=null) {
				CopyPicDest(PictureButton_yourself, sPath);
			}
		} catch (Exception e) {
			Utility.MessageBox(this,e.getMessage());
		}
	}

	protected void getDataExt() throws JSONException {
		if (spinner_ziyuanxiangmu!=null) {
			mJson.put( getString(R.string.title_actual_form_edittext_ziyuanxiangmu), spinner_ziyuanxiangmu.getSelectedItem().toString());
			int selectid = spinner_ziyuanxiangmu.getSelectedItemPosition();
			String value = J_GONGCHENGMINGCHENG_XIANGMU.getJSONObject(selectid).getString("MaterialId");
			mJson.put( getString(R.string.title_actual_form_edittext_ziyuanxiangmu_id), value);
		}
		if (editText_jinqian!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_jinqian), editText_jinqian.getText().toString());
		}		
		if (editText_shuliang!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_shuliang), editText_shuliang.getText().toString());
		}	
		if (editText_danjia!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_tiyou_danjia), editText_danjia.getText().toString());
		}	

	}
	
	@Override
	public void InitControls() {
		super.InitControls();
		spinner_ziyuanxiangmu = (Spinner)findViewById(R.id.spinner_ziyuanxiangmu);
		editText_yourself = (EditText) findViewById(R.id.editText_yourself);
		editText_jinqian= (EditText) findViewById(R.id.editText_jinqian);
		PictureButton_yourself = (PictureButton) findViewById(R.id.PictureButton_yourself);
		editText_shuliang= (EditText) findViewById(R.id.editText_shuliang);
		editText_danjia= (EditText) findViewById(R.id.editText_danjia);
		
		if (spinner_ziyuanxiangmu!=null) {
			if (A_GONGCHENGMINGCHENG_XIANGMU!=null) {
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
						R.layout.simple_spinner_item, 
						A_GONGCHENGMINGCHENG_XIANGMU);
				adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
				spinner_ziyuanxiangmu.setAdapter(adapter);
			}else {
				Utility.MessageBox(this,"项目细目数据没有下载");
				this.finish();
			}
		}
	}
	@Override
	public Boolean IsPassed() {
		if (super.IsPassed()) {
			if (!CheckEditNumber(editText_shuliang)) {
				return false;
			}
			if (!CheckEditNumber(editText_danjia)) {
				return false;
			}
			if (!CheckEditNumber(editText_jinqian)) {
				return false;
			}			
			if (!CheckEditStringMin5(editText_yourself, 2)) {
				return false;
			}
			if (PictureButton_yourself!=null && !PictureButton_yourself.IsExist()) {
				Utility.MessageBox(this, PictureButton_yourself.FIELD );
				return false;
			}
			return true;
		}else {
			return false;
		}
	}

}
